package com.herohead.servers;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.commons.io.FileUtils;

import redis.clients.jedis.Jedis;

import com.herohead.redis.GetRedisServer;

public class GetServerList {

	static Jedis jd;
	static Connection connection = null;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//String fileInput = args[0];

		String fileInput = "c:\\temp\\RVTools_tabvInfo-us40.csv";

		//log.debug(fileInput);
		File file = new File(fileInput);


		GetRedisServer grs = new GetRedisServer();


		String redisHost = grs.getRedisHost();
		//test
		jd = new Jedis(redisHost);

		//jd.rpush("savegroup", sq);
		//Long cnt = jd.incr("savegroup:count");
		//jd.hset(("savegroup"), String.valueOf(cnt), sq);


		//jd.del("AM:servers");
		//jd.del("AM:VM:servers");
		//jd.del("AM:AV:servers");

		String sq = null;
		try {
			sq = FileUtils.readFileToString(file, "UTF-8");
			//System.out.println(sq);
			String[] lines = sq.split("\n");

			for (String s: lines) {
				String server = (s.trim().split(","))[0];
				String poweron = (s.trim().split(","))[2];
				String OS = (s.trim().split(","))[38];
				if ((poweron.contains("poweredOn")) && (OS.contains("Windows"))) {
					if (!server.startsWith("us99")) {
						jd.sadd("AM:VM:servers", server.toLowerCase());
						System.out.println(server + ":" + poweron + ":"+ OS);
					}
				}

			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		try {
			Class.forName("org.postgresql.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			connection = DriverManager.getConnection("jdbc:postgresql://us00s-avamar01.am.mt.mtnet:5555/mcdb","viewuser", "viewuser1");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		String serverSQL = "select  display_client_name, enabled, has_backups " +
				"from v_clients_2 " +
				"where (enabled = true) and (has_backups = true)";


		PreparedStatement sql = null;
		try {
			sql = connection.prepareStatement(serverSQL);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultSet resultSet = null;
		try {
			resultSet = sql.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//String statusCode = "";
		try {
			while (resultSet.next()) {
				String server = (resultSet.getString("display_client_name"));
				System.out.println(server);
				if (server.contains(".")) {
					//System.out.println(server.split("-"));
					server = server.replace(".", "#");
					server = server.split("#")[0];
				}
				server = server.toLowerCase();
				//System.out.println(server);
				if (!server.startsWith("us99")) {
					jd.sadd("AM:AV:servers", server);
				}
				//System.out.print(resultSet.getString("enabled") + " ");
				//System.out.println(resultSet.getString("has_backups"));

				//else System.out.println(statusCode + " " + resultSet.getString("backup_count") + "-" + lookupEvent(connection,statusCode) +  "  This has not been included in calc");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		try {
			sql.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		//GetRedisServer grs = new GetRedisServer();

		//grs.getRedisHost();

		//jd = new Jedis("ch00sm33",6380);

		//jd.rpush("savegroup", sq);
		//Long cnt = jd.incr("savegroup:count");
		//jd.hset(("savegroup"), String.valueOf(cnt), sq);


		//file.delete();


	}

}
